//
//  JZCameraShootingCenterButton.m
//  JZShortVideo
//
//  Created by liujz on 31/05/2018.
//  Copyright © 2018 liujz. All rights reserved.
//

#import "JZCameraShootingCenterButton.h"
#import "ColorManager.h"

@interface JZCameraShootingCenterButton()

@property(nonatomic , strong) UIBezierPath * path;
@property(nonatomic , strong) CAShapeLayer * centerLayer;

@end

@implementation JZCameraShootingCenterButton

+ (JZCameraShootingCenterButton *)getShootingCenterButton:(CGFloat)maxLineW minLineWidth:(CGFloat)minLineW lineColor:(UIColor *)lineColor centerColor:(UIColor *)centerColor TapEnable:(BOOL) tapEnable LongPressEnable:(BOOL) longPressEnable {
    
    // 初始化一个实例对象
    JZCameraShootingCenterButton *centerButton = [[JZCameraShootingCenterButton alloc] initWithFrame:CGRectMake(0, 0, 80, 80) TapEnable:tapEnable LongPressEnable:longPressEnable];
    
    centerButton.centerLayer                = [CAShapeLayer layer];
    centerButton.centerLayer.frame          = centerButton.bounds;
    centerButton.centerLayer.path           = centerButton.path.CGPath;                          // 设置绘制路径
    centerButton.centerLayer.strokeColor    = [ColorManager colorWithRGB:@"#DAD9D6"].CGColor; // 设置划线颜色
    centerButton.centerLayer.fillColor      = [UIColor whiteColor].CGColor;   // 设置填充颜色
    centerButton.centerLayer.lineWidth      = 13;    // 设置线宽
    centerButton.centerLayer.strokeEnd      = 1;     // 设置轮廓结束位置
    [centerButton.layer addSublayer:centerButton.centerLayer];
    
    return centerButton;
}

- (id)initWithFrame:(CGRect)frame TapEnable:(BOOL) tapEnable LongPressEnable:(BOOL) longPressEnable {
    
    if (self = [super initWithFrame:frame]) {
        if (tapEnable) {
            // 单击手势
            UITapGestureRecognizer *singeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleClick)];
            [self addGestureRecognizer:singeTap];
        }
        
        if (longPressEnable) {
            // 长按
            UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
            [self addGestureRecognizer:longTap];
        }
    }
    return self;
}

// 重写布局下子控制器
- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.centerLayer.frame  = self.bounds;
    self.centerLayer.path   = [UIBezierPath bezierPathWithOvalInRect:self.bounds].CGPath; //设置绘制路径

}

#pragma mark - Gestures Action
/**
 长按事件

 @param tap UILongPressGestureRecognizer
 */
- (void)longPress:(UILongPressGestureRecognizer *)tap {
    
    // 手势开始
    if (UIGestureRecognizerStateBegan == tap.state) {
        
        self.centerLayer.lineWidth = 20;
        if ([self.shootingCenterDelegate respondsToSelector:@selector(shootingStart:obj:)]) {
            // 视频开始拍摄
            [self.shootingCenterDelegate shootingStart:LongPressGesture obj:self];
        }
    }
    // 判断手势结束
    if (UIGestureRecognizerStateEnded == tap.state) {
        
        self.centerLayer.lineWidth = 10;
        if ([self.shootingCenterDelegate respondsToSelector:@selector(shootingEnd:obj:)]) {
            // 视频结束拍摄
            [self.shootingCenterDelegate shootingEnd:LongPressGesture obj:self];
        }
    }
}

/**
 单击事件
 */
- (void)singleClick {
    
    if ([self.shootingCenterDelegate respondsToSelector:@selector(shootingStart:obj:)]) {
        // 照片开始拍摄
        [self.shootingCenterDelegate shootingStart:TapGesture obj:self];
    }
    if ([self.shootingCenterDelegate respondsToSelector:@selector(shootingEnd:obj:)]) {
        // 照片结束拍摄
        [self.shootingCenterDelegate shootingEnd:TapGesture obj:self];
    }
}


@end



